﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Flight
{
    static class Graphics
    {
        public static readonly Texture2D noise, blank;

        static Graphics()
        {
            blank = new Texture2D(FlightGame.game.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
            blank.SetData(new[] { Color.White });
            string path = System.IO.Path.Combine(Settings.BasePath, "images", "quad-noise.png");
            noise = Texture2D.FromStream(FlightGame.game.GraphicsDevice,
                System.IO.File.OpenRead(
                    System.IO.Path.Combine(Settings.BasePath, "images", "quad-noise.png")));
        }

        public static void line(
            Color color, 
            float width, float offset,
            Vector2 point1, Vector2 point2)
        {
            float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
            float length = Vector2.Distance(point1, point2);

            Matrix pretranslation = Matrix.CreateTranslation(0, offset - width / 2, 0);
            Matrix rotation = Matrix.CreateRotationZ(angle);
            Matrix translation = Matrix.CreateTranslation(point1.X, point1.Y, 0);
            Matrix transform = pretranslation * rotation * translation;

            FlightGame.game.drawQuad(
                noise, color, 
                transform, length, width, 
                0, length / noise.Width, 0, width / noise.Height);
        }
        public static void line(
            Color color, 
            float width, float offset, 
            float x1, float y1, float x2, float y2)
        {
            Graphics.line(color, width, offset, new Vector2(x1, y1), new Vector2(x2, y2));
        }

        public static void line2d(
           this SpriteBatch batch, Color color,
           float width, 
           Vector2 point1, Vector2 point2)
        {
            float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
            float length = Vector2.Distance(point1, point2);

            batch.Draw(blank, point1, null, color,
                angle, Vector2.Zero, new Vector2(length, width),
                SpriteEffects.None, 0);
        }
        public static void line2d(
            this SpriteBatch batch, Color color,
            float width, 
            float x1, float y1, float x2, float y2)
        {
            batch.line2d(color, width, new Vector2(x1, y1), new Vector2(x2, y2));
        }

        public static void polygon(Color color, Polygon polygon, Vector2 translation)
        {
            FlightGame.game.draw(Matrix.CreateTranslation(translation.X, translation.Y, 0),
                noise, color,
                polygon.vertexBuffer, polygon.vertices);
        }
    }

    class Polygon
    {
        public readonly Vector2[] points;

        public readonly VertexPositionNormalTexture[] vertices;
        public readonly VertexBuffer vertexBuffer;

        public Polygon(params Vector2[] points)
        {
            this.points = points;
            
            vertices = new VertexPositionNormalTexture[points.Length + 1];

            for (int i = 0; i < points.Length; i++)
            {
                vertices[i] = new VertexPositionNormalTexture(
                    new Vector3(points[i], 0),
                    new Vector3(0, 0, 1),
                    new Vector2(points[i].X / Graphics.noise.Width, points[i].Y / Graphics.noise.Height));
            }
            vertices[vertices.Length - 1] = vertices[0];

            vertexBuffer = new VertexBuffer(FlightGame.game.GraphicsDevice,
                VertexPositionNormalTexture.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
            vertexBuffer.SetData(vertices);
        }
    }
}
